package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Hdtemplate;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.MerchantApp;
import com.qianniu.napi.admin.service.IHdtemplateService;
import com.qianniu.napi.admin.service.IRedisService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * <p>
 * </p>
 *
 * @author chenxuebin
 * @since 2020-04-25
 */
@Controller
public class HdtemplateController {

    private static final Logger logger = LoggerFactory.getLogger(HdtemplateController.class);

    @Autowired
    private IHdtemplateService hdtemplateService;
    @Autowired
    private IRedisService redisService;

    @RequestMapping("/api/hdtemplate/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value = "type", required = false) String type,
                      @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                      @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        Jinfo info = new Jinfo();
        if (pageSize == null) pageSize = 10;
        Page page = new Page(pageIndex, pageSize);
        MerchantApp mapp = redisService.getMApp();
        Long mpid = mapp.getVersion().intValue() == 9 && mapp.getAsmpid() != null ? mapp.getAsmpid() : ObjectUtil.getMpid();

        Wrapper<Hdtemplate> entity = new EntityWrapper<Hdtemplate>()
                .eq("mpid", mpid)
                .eq("state", 1).orderBy("uses");

        if (!Strings.isNullOrEmpty(type)) entity.eq("type", type);

        page = hdtemplateService.selectPage(page, entity);
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/hdtemplate/add")
    @ResponseBody
    public Jinfo hdtemplateAdd(@Valid Hdtemplate hdtemplate,
                               BindingResult bindingResult, HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            info.setMsg("There are errors! " + bindingResult);
            info.setCode(400);
            return info;
        }
        hdtemplate.setCreatetime(new Date());
        hdtemplate.setMid(ObjectUtil.getMid());
        hdtemplate.setMpid(ObjectUtil.getMpid());
        hdtemplate.insert();
        return info;
    }

    @RequestMapping("/api/hdtemplate/modify")
    @ResponseBody
    public Jinfo hdtemplateUpdate(HttpServletRequest req,
                                  Hdtemplate hdtemplate) {
        Jinfo info = new Jinfo();
        hdtemplate.setModifytime(new Date());
        hdtemplate.updateById();
        return info;
    }

    //活动模板详情接口
    //参数：id
    @RequestMapping("/api/hdtemplate/detail")
    @ResponseBody
    public Jinfo getHdtemplateDetailById(@RequestParam("id") Long id) {
        Jinfo info = new Jinfo();
        if (id == null) {
            info.setCode(1);
            info.setMsg("参数有误");
            return info;
        }
        Hdtemplate hdtemplate = hdtemplateService.selectById(id);
        info.setInfo(hdtemplate);
        return info;
    }

}
